home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / net / serverso.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  4.2 KB  |  158 lines

  1. /*
  2.  * @(#)ServerSocket.java    1.14 95/10/24 Jonathan Payne
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.net;
  21.  
  22. import java.io.IOException;
  23.  
  24. /**
  25.  * The server Socket class. It uses a SocketImpl
  26.  * to implement the actual socket operations. It is done this way 
  27.  * so that you are able to change socket implementations depending 
  28.  * on the kind of firewall that is used. You can change socket
  29.  * implementations by setting the SocketImplFactory.
  30.  *
  31.  * @version     1.14, 10/24/95
  32.  * @author     Jonathan Payne
  33.  * @author     Arthur van Hoff
  34.  */
  35. public final 
  36. class ServerSocket {
  37.     /**
  38.      * The implementation of this Socket.
  39.      */
  40.     SocketImpl impl;
  41.  
  42.     /**
  43.      * Creates an unconnected server socket. Note: this method
  44.      * should not be public.
  45.      */
  46.     ServerSocket() throws IOException {
  47.     impl = (factory != null) ? factory.createSocketImpl() : new PlainSocketImpl();
  48.     }
  49.  
  50.     /**
  51.      * Creates a server socket on a specified port.
  52.      * @param port the port
  53.      */
  54.     public ServerSocket(int port) throws IOException {
  55.     this(port, 50);
  56.     }
  57.  
  58.     /**
  59.      * Creates a server socket, binds it to the specified local port 
  60.      * and listens to it.  You can connect to an annonymous port by 
  61.      * specifying the port number to be 0.
  62.      * @param port the specified port
  63.      * @param count the amt of time to listen for a connection
  64.      */
  65.     public ServerSocket(int port, int count) throws IOException {
  66.     this();
  67.  
  68.     SecurityManager security = System.getSecurityManager();
  69.     if (security != null) {
  70.         security.checkListen(port);
  71.     }
  72.  
  73.     impl.create(true);
  74.     impl.bind(InetAddress.anyLocalAddress, port);
  75.     impl.listen(count);
  76.     }
  77.  
  78.     /**
  79.      * Gets the address to which the socket is connected.
  80.      */
  81.     public InetAddress getInetAddress() {
  82.     return impl.address;
  83.     }
  84.  
  85.     /**
  86.      * Gets the port to which the socket is listening on
  87.      */
  88.     public int getLocalPort() {
  89.     return impl.localport;
  90.     }
  91.  
  92.     /**
  93.      * Accepts a connection. This method will block until the
  94.      * connection is made.
  95.      */
  96.     public Socket accept() throws IOException {
  97.     Socket s = new Socket();
  98.  
  99.     try {
  100.         //s.impl.create(true);
  101.         s.impl.address = new InetAddress();
  102.         impl.accept(s.impl);
  103.  
  104.         SecurityManager security = System.getSecurityManager();
  105.         if (security != null) {
  106.         security.checkAccept(s.getInetAddress().getHostName(),
  107.                      s.getPort());
  108.         }
  109.     } catch (IOException e) {
  110.         s.close();
  111.         throw e;
  112.     } catch (SecurityException e) {
  113.         s.close();
  114.         throw e;
  115.     }
  116.     
  117.     return s;
  118.     }
  119.  
  120.     /**
  121.      * Closes the server socket.
  122.      */
  123.     public void close() throws IOException {
  124.     impl.close();
  125.     }
  126.  
  127.     /**
  128.      * Returns the implementation address and implementation port of 
  129.      * this ServerSocket as a String.
  130.      */
  131.     public String toString() {
  132.     return "ServerSocket[fd=" + (impl.fd-1) + ",addr=" + impl.address + ",localport=" + impl.localport + "]";
  133.     }
  134.  
  135.     /**
  136.      * The factory for all server sockets.
  137.      */
  138.     private static SocketImplFactory factory;
  139.  
  140.     /**
  141.      * Sets the system's server SocketImplFactory. The factory can 
  142.      * be specified only once.
  143.      * @param fac the desired factory
  144.      * @exception SocketException If the factory has already been 
  145.      * defined.
  146.      */
  147.     public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException {
  148.     if (factory != null) {
  149.         throw new SocketException("factory already defined");
  150.     }
  151.     SecurityManager security = System.getSecurityManager();
  152.     if (security != null) {
  153.         security.checkSetFactory();
  154.     }
  155.     factory = fac;
  156.     }
  157. }
  158.